//
//  SuitThumbView.m
//  PurchasingMeeting
//
//  Created by yanseng.lin on 12/4/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "SuitThumbView.h"
#import "TKImageCache.h"

@implementation SuitThumbView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithProduct:(SuitObject *)object tag:(int)tag type:(SuitThumbView_obj)type
{
    numtag = tag;
    suit = object;
    
    imageCache_ = [[TKImageCache alloc] initWithCacheDirectoryName:@"image"];
    imageCache_.notificationName = @"suit_image";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(suit_image:) name:@"suit_image" object:nil];
    
    UIImage *img = [[UIHelper getInstance] getBundleImage:@"product_thumb_bg.png"];
#ifdef DHH_iOS
    self = [self initWithFrame:CGRectMake(0, 0, 145, 150)];
#else
    if (type==SuitView_obj) {
        self = [self initWithFrame:CGRectMake(0, 0, img.size.width, img.size.height)];
    }
    else
    {
        self = [self initWithFrame:CGRectMake(0, 0, 320, 320)];
    }
    
#endif
   // self.backgroundColor = [UIColor grayColor];
//    NSString *url = [NSString stringWithFormat:@"_%@",suit.pic_path];
//    NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
//    [imageCache_ imageForKey:suit.pic_path url:[NSURL URLWithString:path] queueIfNeeded:YES tag:99+100*suit.n_id];
    UIImageView *bgV = [[UIImageView alloc] initWithImage:img];
#ifdef DHH_iOS
    bgV.frame = self.frame;
#endif
    bgV.frame = self.frame;
    [self addSubview:bgV];
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click)];
    [self addGestureRecognizer:gesture];
    
    self.imageview = [[UIImageView alloc]initWithImage:img];
#ifdef DHH_iOS
    self.imageview.frame = CGRectMake(0, 0, 145, 150);
#else
    self.imageview.frame = CGRectMake(0, 0, self.width, self.height);
#endif
    self.imageview.contentMode = UIViewContentModeScaleAspectFit;
    self.imageview.clipsToBounds = YES;
    [self addSubview:self.imageview];
    //[self loadImage];
    
#ifdef DHH_iOS
    self.detail = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 145, 40)];
#else
    self.detail = [[UILabel alloc]initWithFrame:CGRectMake(0, self.height, self.width, 30)];
    if (type==SuitView_obj) {
        self.detail.height = 30;
    }
    else
    {
        self.detail.height = 60;
    }
#endif
    self.detail.bottom = self.imageview.bottom;
    self.detail.text = suit.c_mingc;
    self.detail.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
    self.detail.textColor = [UIColor whiteColor];
    self.detail.font = [UIFont systemFontOfSize:17];
    self.detail.textAlignment = NSTextAlignmentCenter;
  //  CGSize s = [self.detail.text sizeWithFont:self.detail.font];
    self.detail.numberOfLines = 0;
    [self addSubview:self.detail];
    return self;
}

- (void)loadImage{
    NSString *url = [NSString stringWithFormat:@"_%@",suit.pic_path];
    NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url];
    UIImage *img = [imageCache_ imageForKey:url url:[NSURL URLWithString:path] queueIfNeeded:YES tag:99+100*suit.n_id];
    self.imageview.image = img;
    /*
    if (img) {
        self.detail.width = img.size.width/(img.size.height/self.imageview.height);
        self.detail.center = CGPointMake(self.imageview.width/2, 0);
        self.detail.bottom = self.imageview.height;
    }
*/
    // suitImageView_.image = [[UIHelper getInstance] getBundleImage:@"detail.png"];
    /*
    UIImage *img2 = [[UIHelper getInstance] getBundleImage:@"collect_image.png"];
    UIButton *collect = [UIButton buttonWithType:UIButtonTypeCustom];
    
    ShopObject *shopobj = [[DataManager sharedManager]getCurrentShop];
    if ([[DataManager sharedManager]productIssuitFavorite:suit Shopid:shopobj.shopID]>0) {
        [collect setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"collect_redimage.png"] forState:UIControlStateNormal];
    }
    else
    {
        [collect setBackgroundImage:img2 forState:UIControlStateNormal];
    }
    collect.frame = CGRectMake(0, 0, img2.size.width, img2.size.height);
    
    // collect.right = productImageV.right-5;
    collect.top=10;
    collect.right = self.imageview.right-40;
    collect.tag = 181000;
    [collect addTarget:self action:@selector(collect:) forControlEvents:UIControlEventTouchUpInside];
    [suitImageView_ addSubview:collect];
    */
    /*
    for (int i = 0; i < suit_.components.count; i++) {
        UIView *v = [self viewWithTag:10+i];
        UIView *vv = (UIView *)[v viewWithTag:500];
        UIImageView *imgV = (UIImageView *)[vv viewWithTag:1000];
        
        SubSuitObject *obj = [suit_.components objectAtIndex:i];
        //        ProductObject *pObj = [[DataManager sharedManager] getProduct:obj.productCode withColorID:obj.colorid];
        
        NSString *url1 = [NSString stringWithFormat:@"_%@",obj.productImage];
        NSString *path = [[[PMGlobal getInstance] getImageURL] stringByAppendingPathComponent:url1];
        UIImage *img = [imageCache_ imageForKey:url1 url:[NSURL URLWithString:path] queueIfNeeded:YES tag:i+100*suit_.n_id];
        imgV.image = img;
    }
    */
}

- (void)unloadImage{
    [imageCache_ cancelOperations];
    self.imageview.image = nil;
    /*
    for (int i = 0; i < suit_.components.count; i++) {
        UIView *v = [self viewWithTag:10+i];
        UIView *vv = (UIView *)[v viewWithTag:100];
        UIImageView *imgV = (UIImageView *)[vv viewWithTag:1000];
        imgV.image = nil;
    }
     */
}


- (void)suit_image:(NSNotification *)notification{
    NSDictionary *dict = [notification userInfo];
    UIImage *img = [dict objectForKey:@"image"];
    /*
    if (img) {
        self.detail.width = img.size.width/(img.size.height/self.imageview.height);
        self.detail.center = CGPointMake(self.imageview.width/2, 0);
        self.detail.bottom = self.imageview.height;
    }
     */
    int tag = [[dict objectForKey:@"tag"] intValue];
    if (tag == 99+100*suit.n_id) {
        self.imageview.image = img;
    }
    
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)click
{
    if (numtag==0) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"detail" object:suit];
    }
    else if(numtag ==1){
        [[NSNotificationCenter defaultCenter]postNotificationName:@"detail1" object:suit];
    }else if(numtag==2){
        [[NSNotificationCenter defaultCenter]postNotificationName:@"suit_detail" object:suit];
        
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
